package vg.modules.navigator.components;


import com.google.common.collect.Lists;
import vg.modules.navigator.components.popup_menu_component.PopupMenuComponent;
import vg.services.user_interface_manager.interfaces.AUserInterfaceElement;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.List;

public class NavigatorMainComponent extends AUserInterfaceElement {
	// Main components
    private JPanel outView, innerView;

	private List<SmartTreeComponent> smartTreeComponents;
	private JButton searchButton;
	private final JTextField searchField;
    private final PopupMenuComponent popupMenuComponent;

    private final PropertyChangeListener progressListener;

	// Main data

	// Mutex
	private final Object generalMutex = new Object();

	public NavigatorMainComponent() {
        super("Navigator", null);

        outView = new JPanel(new GridLayout(1, 1));
        innerView = new JPanel(new GridBagLayout());
        outView.add(innerView);

        smartTreeComponents = Lists.newArrayList();

        popupMenuComponent = new PopupMenuComponent();
        searchField = new JTextField();

        // add listeners
        searchField.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                synchronized (generalMutex) {
                    search();
                }
            }
        });

        searchButton = new JButton(new ImageIcon("./data/resources/textures/navigator/next.png"));
        searchButton.setToolTipText("Search");
        searchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                synchronized (generalMutex) {
                    search();
                }
            }
        });

        progressListener = new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                //bar.setValue((Integer) evt.getNewValue());
            }
        };

        rebuildView();
	}

    public void addGraphModel(int graphModelId) {
        synchronized (generalMutex) {
            SmartTreeComponent smartTreeComponent = new SmartTreeComponent(graphModelId, progressListener);
            smartTreeComponents.add(smartTreeComponent);
            popupMenuComponent.addTree(smartTreeComponent.getTree());
            rebuildView();
        }
    }

    @Override
	public JComponent getView() {
		return outView;
	}

//=============================================================================
//-----------PRIVATE METHODS---------------------------------------------------
    private void rebuildView() {
        innerView.removeAll();

        JPanel treePanel = new JPanel(new GridBagLayout());
        int verticalIndex = 0;
        for (SmartTreeComponent smartTreeComponent : smartTreeComponents) {
            treePanel.add(smartTreeComponent.getView(), new GridBagConstraints(0,verticalIndex++,  1,1,  1,0,  GridBagConstraints.NORTH, GridBagConstraints.HORIZONTAL, new Insets(0, 0, 0, 0),  0,0));
        }
        treePanel.add(new JPanel(), new GridBagConstraints(0,verticalIndex,  1,1,  1,1,  GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0),  0,0));
        JScrollPane treeScrollPane = new JScrollPane(treePanel);
        innerView.add(treeScrollPane,
                new GridBagConstraints(0, 0, 2, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));

        innerView.add(searchField,
                new GridBagConstraints(0, 1, 1, 1, 1, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));

        innerView.add(searchButton,
                new GridBagConstraints(1,1,  1,1,  0,0,  GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(3, 3, 3, 3),  0,0));

        innerView.updateUI();
    }

    private void search() {
        for (SmartTreeComponent smartTreeComponent : smartTreeComponents) {
            smartTreeComponent.search(searchField.getText());
        }
    }
}